package one.stand.controller;

import com.jmp.base.dto.LiveBackRequest;
import com.jmp.base.dto.LiveEndDto;
import com.jmp.dto.*;
import com.jmp.feign.LiveFeign;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author cjr
 */
@Api(description = "腾讯云直播回调")
@RestController
@RequestMapping("live")
@Slf4j
public class LiveController {
    @Autowired
    private LiveFeign liveFeign;

    /**
     * 推流
     */
    @PostMapping("/begin")
    public LiveResponse end(@RequestBody LiveBeginRequest request, HttpServletResponse response) {

        if (!request.check()) {
            return error(response);
        }

        liveFeign.end(LiveEndDto.builder()
                .roomId(Integer.valueOf(request.getStream_id()))
                .end(false)
                .build());

        return LiveResponse.builder().code(0).build();
    }

    /**
     * 断流
     */
    @PostMapping("/end")
    public LiveResponse end(@RequestBody LiveEndRequest request, HttpServletResponse response) {

        if (!request.check()) {
            return error(response);
        }

        liveFeign.end(LiveEndDto.builder()
                .roomId(Integer.valueOf(request.getStream_id()))
                .end(true)
                .build());

        return LiveResponse.builder().code(0).build();
    }

    /**
     * 录制
     */
    @PostMapping("/replay")
    public LiveResponse replay(@RequestBody LiveReplayRequest request, HttpServletResponse response) {

        if (!request.check()) {
            return error(response);
        }

        try {
            LiveBackRequest backRequest = LiveBackRequest.builder()
                    .roomId(Integer.valueOf(request.getStream_id()))
                    .fileId(request.getFile_id())
                    .replayUrl(request.getVideo_url())
                    .build();
            liveFeign.back(backRequest);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return error(response);
        }

        return LiveResponse.builder().code(0).build();
    }

    /**
     * 截图
     */
    @PostMapping("/screenshot")
    public LiveResponse screenshot(@RequestBody LiveScreenshotRequest request, HttpServletResponse response) {

        if (!request.check()) {
            return error(response);
        }

        try {
            LiveBackRequest backRequest = LiveBackRequest.builder()
                    .roomId(Integer.valueOf(request.getStream_id()))
                    .screenshotUrl(request.getPic_full_url())
                    .build();
            liveFeign.back(backRequest);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return error(response);
        }

        return LiveResponse.builder().code(0).build();
    }

    private LiveResponse error(HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(500);
        return LiveResponse.builder().code(-1).build();
    }
}
